<template>
  <div id="repair">
    <el-container>
      <!-- 内容 -->
      <el-main>
        <el-row :gutter="24">
          <el-col :span="6">
            <div class="grid-content bg-purple">
              <el-form ref="form" :model="audit" label-width="100px">
                <el-form-item label="报修主题：">
                  <el-input v-model="audit.repair_bxzt"></el-input>
                </el-form-item>
              </el-form>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple-light">
              <el-form ref="form" :model="audit" label-width="100px">
                <el-form-item label="报修人：">
                  <el-input v-model="audit.repair_xm"></el-input>
                </el-form-item>
              </el-form>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">
              <el-form ref="form" :model="audit" label-width="100px">
                <el-form-item label="状态：">
                  <el-select v-model="audit.repair_zt" placeholder="请选择">
                    <el-option :label="v" :value="v" v-for="v in ztDate" :key="v">{{v}}</el-option>
                  </el-select>
                </el-form-item>
              </el-form>
            </div>
          </el-col>
          <el-col :span="5" :offset="1">
            <div class="grid-content bg-purple">
              <el-button type="primary" @click="onSubmit">查询</el-button>
              <el-button type="info" @click="onReset">重置</el-button>
            </div>
          </el-col>
        </el-row>
        <!-- 表格 -->
        <el-row :gutter="24" class="primary">
          <el-col :span="24">
            <div class="grid-content bg-purple">
              <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="repair_id" label="序号" width="120">
                </el-table-column>
                <el-table-column prop="repair_dh" label="报修单号" width="140">
                </el-table-column>
                <el-table-column prop="repair_bxzt" label="报修主题" width="180">
                </el-table-column>
                <el-table-column prop="repair_wz" label="报修位置" width="180">
                </el-table-column>
                <el-table-column prop="repair_nr" label="报修内容" width="140">
                </el-table-column>
                <el-table-column prop="repair_xm" label="报修人" width="140">
                </el-table-column>
                <el-table-column prop="repair_rq" label="报修日期" width="200">
                </el-table-column>
                <el-table-column prop="repair_zt" label="状态">
                </el-table-column>
                <el-table-column prop="operation" label="操作">
                  <template slot-scope="scope">
                    <el-button @click="Modify(scope.row)" type="text">查看详情</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-col>
        </el-row>
      </el-main>

      <!-- 页码 -->
      <el-footer>
        <page class="page"></page>
      </el-footer>
      <el-dialog title="报修详情" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
        <div id="drom_up">
          <el-container>
            <el-header id="header" style="height:40px">
              <p>报修详情</p>
            </el-header>
            <el-main>
              <el-row :gutter="24">
                <el-col :span="12">
                  <div class="grid-content bg-purple">
                    <el-col :span="6">
                      <span>报修主题：</span>
                    </el-col>
                    <el-col :span="18">
                      <span>{{Modifytable.repair_bxzt}}</span>
                    </el-col>
                  </div>
                </el-col>
                <el-col :span="12">
                  <div class="grid-content bg-purple-light">
                    <el-col :span="6">
                      <span>报修位置：</span>
                    </el-col>
                    <el-col :span="18">
                      <span>{{Modifytable.repair_wz}}</span>
                    </el-col>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <div class="grid-content bg-purple">
                    <el-col :span="6">
                      <span>报修内容：</span>
                    </el-col>
                    <el-col :span="18">
                      <span>{{Modifytable.repair_nr}}</span>
                    </el-col>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <div class="grid-content bg-purple">
                    <el-col :span="6">
                      <span>报修人：</span>
                    </el-col>
                    <el-col :span="18">
                      <span>{{Modifytable.repair_xm}}</span>
                    </el-col>
                  </div>
                </el-col>
                <el-col :span="12">
                  <div class="grid-content bg-purple-light">
                    <el-col :span="6">
                      <span>联系电话：</span>
                    </el-col>
                    <el-col :span="18">
                      <span>1{{code}}{{code1}}{{code2}}</span>
                      <el-button type="primary" @click="onAccording" id="button">显示</el-button>
                    </el-col>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <div class="grid-content bg-purple">
                    <el-col :span="6">
                      <span>报修时间：</span>
                    </el-col>
                    <el-col :span="18">
                      <span>{{Modifytable.repair_rq}}</span>
                    </el-col>
                  </div>
                </el-col>
              </el-row>
              <hr>
              <el-row :gutter="24">
                <el-col :span="24">
                  <div class="grid-content bg-purple-light">
                    <el-col :span="12">
                      <el-form ref="form" :model="deal" label-width="80px">
                        <el-form-item label="处理：">
                          <el-input v-if="Modifytable.repair_zt=='已处理'" value="已处理" disabled>已处理</el-input>
                          <el-input v-else v-model="deal.results"></el-input>
                        </el-form-item>
                      </el-form>
                    </el-col>
                    <el-col :span="12">
                      <el-col :span="15">
                        <el-form ref="form" :model="Modifytable" label-width="100px">
                          <el-form-item label="产生的费用：">
                            <el-input v-show="Modifytable.repair_zt=='已处理'?true:false" v-model="Modifytable.repair_money" disabled></el-input>
                            <el-input v-show="Modifytable.repair_zt=='已处理'?false:true" v-model="Modifytable.repair_money" oninput="value=value.replace(/\D/g,'')"></el-input>
                            <span>{{Modifytable.cost}}元</span>
                          </el-form-item>
                        </el-form>
                      </el-col>
                      <el-col :span="4">
                        <el-button type="primary" @click="onMention">提交</el-button>
                      </el-col>
                    </el-col>
                  </div>
                </el-col>
              </el-row>
              <hr>
            </el-main>
          </el-container>
        </div>
        <div id="footer">
          <p>处理记录</p>
          <el-main>
            <el-row :gutter="24">
              <el-col :span="6">
                <div class="grid-content bg-purple">
                  <el-col :span="13">
                    <span>处理人：</span>
                  </el-col>
                  <el-col :span="11">
                    <span>admin</span>
                  </el-col>
                </div>
              </el-col>
              <el-col :span="9">
                <div class="grid-content bg-purple">
                  <el-col :span="6">
                    <span>结果：</span>
                  </el-col>
                  <el-col :span="18">
                    <span>处理产生费用：{{Modifytable.repair_money}}元</span>
                  </el-col>
                </div>
              </el-col>
              <el-col :span="9">
                <div class="grid-content bg-purple">
                  <el-col :span="6">
                    <span>时间：</span>
                  </el-col>
                  <el-col :span="16">
                    <span>{{value}}</span>
                  </el-col>
                </div>
              </el-col>
            </el-row>
          </el-main>
        </div>
      </el-dialog>
    </el-container>

  </div>
</template>

<script>
import page from '../../components/page.vue';
import Event from '../../utils/Event.js';
export default {
  components: { page },
  data() {
    return {
      code: '',
      code2: '',
      code1: '****',
      value: '',
      tableData: [],
      tolDate: [],
      toltable: [],
      dialogVisible: false,
      deal: {
        results: '',
        cost: '',
      },
      page: 1,
      count: 4,
      ztDate: [],
      repair: {
        dh: '',
        bxzt: '',
        wz: '',
        nr: '',
        xm: '',
        rq: '',
        zt: '',
      },
      //详情数据
      Modifytable: {
        repair_dh: '',
        repair_bxzt: '',
        repair_wz: '',
        repair_nr: '',
        repair_xm: '',
        repair_rq: '',
        repair_zt: '',
        repair_money: ''
      },
      audit: {
        repair_bxzt: '',
        repair_xm: '',
        repair_zt: ''
      }
    }
  },
  methods: {
    //查询
    onSubmit() {
      for (let j in this.audit) {
        if (this.audit[j].trim()) {
          let arr = this.toltable.filter((i) => {
            return i[j].indexOf(this.audit[j]) != -1
          })
          Event.$emit('noticeData', arr)
        }
      }
    },
    //重置
    onReset() {
      this.audit = {}
      this.getuer()
    },

    //查看详情框
    Modify(data) {
      this.dialogVisible = true;
      this.Modifytable = { ...data };
      this.createCode()
      this.Modifytable.cost = this.tableData.repair_money
    },
    // 随机电话号码
    createCode() {
      let code1 = ''
      let code2 = ''

      // 设置长度
      let codelength1 = 2
      let codelength2 = 4
      //  设置随机字符
      let readom = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
      // 循环数组
      for (let i = 0; i < codelength1; i++) {
        let index = Math.floor(Math.random() * 9)
        code1 += readom[index]
      }
      for (let j = 0; j < codelength2; j++) {
        let index = Math.floor(Math.random() * 9)
        code2 += readom[index]
      }
      this.code = code1
      this.code2 = code2
    },
    // 显示电话号码
    onAccording() {
      let code4 = ''
      let codelength = 4
      let readom = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
      for (let i = 0; i < codelength; i++) {
        let index = Math.floor(Math.random() * 9)
        code4 += readom[index]
      }
      if (this.code1 == '****') {
        this.code1 = code4
      } else {
        this.code1 = this.code1
      }
    },
    // 详情框的提交
    onMention() {
      this.Modifytable.cost = this.deal.cost
      this.$axios({
        url: '/repairs/change',
        method: 'post',
        data: {
          dh: this.Modifytable.repair_dh,
          money: this.Modifytable.repair_money
        }
      }).then((res) => {
        if (res.data.code == 200) {
          this.$message({ message: '提交成功', type: 'success' })
          this.dialogVisible = false
          this.getuer()
        }
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
      this.code1 = '****'
    },
    getuer() {
      this.$axios({
        url: '/repairs/sel',
        method: 'get',
      }).then((res) => {
        Event.$emit('table', res.data.data)
        this.toltable = res.data.data
        this.tableData = this.toltable
        for (let i = 0; i < this.tableData.length; i++) {
          this.ztDate.push(this.toltable[i].repair_zt)
          this.ztDate = [...new Set(this.ztDate)]
        }
      })
    },
    receive() {
      Event.$on('tableData', v => {
        this.tableData = v.tableData
      })
    }
  },
  //挂载后
  created() {
    this.getuer()
    let aData = new Date()
    let month = aData.getMonth() < 10 ? "0" + (aData.getMonth() + 1) : aData.getMonth() + 1;
    let date = aData.getDate() < 10 ? "0" + aData.getDate() : aData.getDate()
    let date1 = aData.getHours() < 10 ? "0" + aData.getHours() : aData.getHours()
    this.value = aData.getFullYear() + "-" + month + "-" + date + " " + date1 + ":" + aData.getMinutes() + ":" + aData.getSeconds();
  },
  // 数据更新前
  beforeUpdate() {
    this.receive()
  },

}
</script>

<style lang="less" scoped>
#repair {
  padding: 50px;
  height: 100%;
  .el-select {
    width: 200px;
  }
  .el-input {
    width: 200px;
  }
  #daterange {
    width: 300px;
  }
  .el-table {
    margin-top: 50px;
  }
  #button {
    margin-left: 10px;
  }
  .el-row {
    height: 43px;
    line-height: 43px;
  }
}
#header {
  z-index: 99999;
  height: 40px;
  line-height: 40px;
}
#drom_up {
  .el-main {
    padding-top: 0;
    padding-bottom: 0;
  }
  .el-select {
    width: 200px;
  }
  .el-input {
    width: 100px;
  }
  p {
    font-weight: bolder;
  }
  hr {
    margin: 30px 0;
  }
}
#footer {
  p {
    padding-left: 20px;
    height: 40px;
    line-height: 40px;
    font-weight: bolder;
  }
  .el-col {
    z-index: 99999;
    padding: 0;
  }
}
.page {
  text-align: center;
  margin-top: 30px;
}
</style>